using System;
using System.Drawing;
using System.Windows.Forms;

class Program {
    static void Main(){
        Application.Run(new FormSize());
    }

}

class FormSize : Form {
    public FormSize(){
        Text = "FormSize";
        // BackColor = Color.White;
        BackColor = SystemColors.Control;
        ForeColor = SystemColors.ControlText;

        // 大小改变后 整个客户区无效
        ResizeRedraw = true;
    }

    protected override void OnMove(EventArgs e)
    {   
        // 使客户区无效 重新绘制onPaint
        Invalidate();
    }

    protected override void OnResize(EventArgs e)
    {
        Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        var g = e.Graphics;
        // disPlayAttr(g);
        // randColor(g);
        // textAligent(g);
        measureStringDemo(g);
    }

    void disPlayAttr(Graphics g){
        string str = "Location:" + Location  +"\n"+
                    "Size:"      + Size      +"\n"+
                    "Bounds:"    + Bounds    +"\n"+
                    "Width:"     + Width     +"\n"+
                    "Height:"    + Height    +"\n"+
                    "Left:"      + Left +"\n"+
                    "Right:"     + Right +"\n"+
                    "Top:"       + Top +"\n"+
                    "Bottom:"    + Bottom +"\n"+
                    "DesktopLocation:" + DesktopLocation +"\n"+
                    "DesktopBounds:" + DesktopBounds +"\n"+
                    "ClientSize:" + ClientSize +"\n"+
                    "ClientRectangle:" + ClientRectangle +"\n";

        g.DrawString(str,Font,Brushes.Black,0,0);
    }

    void randColor(Graphics g){
        // 随机颜色 OnResize() 中去掉Invalidate
        // 
        var rand = new Random();
        g.Clear(Color.FromArgb(rand.Next(256),rand.Next(256),rand.Next(256)));
    }

    void textAligent(Graphics g){
        var b = new SolidBrush(ForeColor);

        var strfmt = new StringFormat();
        strfmt.Alignment = StringAlignment.Near;
        strfmt.LineAlignment = StringAlignment.Near;
        g.DrawString("LeftTop左上",Font,b,0,0,strfmt);

        strfmt.Alignment = StringAlignment.Far;
        strfmt.LineAlignment = StringAlignment.Near;
        g.DrawString("RightTop右上",Font,b,ClientSize.Width,0,strfmt);

        strfmt.Alignment = StringAlignment.Near;
        strfmt.LineAlignment = StringAlignment.Far;
        g.DrawString("LeftBottom左下",Font,b,0,ClientSize.Height,strfmt);

        strfmt.Alignment = StringAlignment.Far;
        strfmt.LineAlignment = StringAlignment.Far;
        g.DrawString("RightBottom右下",Font,b,ClientSize.Width,ClientSize.Height,strfmt);

        strfmt.Alignment = StringAlignment.Center;
        strfmt.LineAlignment = StringAlignment.Center;
        g.DrawString("Hello World",Font,b,ClientSize.Width/2,ClientSize.Height/2,strfmt);
    }

    void measureStringDemo(Graphics g){
        var str = "Hello World";
        var sizefText = g.MeasureString(str,Font);
        var top = ClientSize.Width - sizefText.Width;

        g.DrawString(str,Font,Brushes.Black,
            (ClientSize.Width - sizefText.Width)/2,
            (ClientSize.Height - sizefText.Height)/2
        );
    }

    void rectCenter(Graphics g){
        var fmt = new StringFormat();
        fmt.Alignment = StringAlignment.Center;
        fmt.LineAlignment = StringAlignment.Center;

        g.DrawString("Hello World!",Font,Brushes.Black,ClientRectangle,fmt);
    }
}